<?php
/**
 * Application level View Helper
 *
 * This file is application-wide helper file. You can put all
 * application-wide helper-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.View.Helper
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Helper', 'View');

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       app.View.Helper
 */
class AppHelper extends Helper {

    function covertDate($date = null, $type = 'mysql')
    {
        $res = '';
        if ($type == 'mysql') {
            $res = date("Y-m-d", strtotime($date));
        }
        elseif ($type == 'mysql_flip') {
            $res = date("d-m-Y", strtotime($date));
        }
        elseif ($type == 'ui') {
            $res = date("F d, Y", strtotime($date));
        }
        elseif ($type == 'ui_short') {
            $res = date("F, Y", strtotime($date));
        }
        elseif ($type == 'uis') {
            $res = date("M d, Y", strtotime($date));
        }
        elseif($type == 'formal'){
            $res = date("m/d/Y", strtotime($date));
        }
        return $res;
    }


    function count_time_between_dates($smaller_date, $bigger_date,$count_type='minute')
    {
        $d1 = strtotime($smaller_date); //Smaller
        $d2 = strtotime($bigger_date); //Bigger
        $min_date = min($d1, $d2);
        $max_date = max($d1, $d2);
        $i = 0;
        $type = "+1 minute";
        if($count_type == 'hour'){
            $type = "+1 hours";
        }
        elseif($count_type == 'day'){
            $type = "+1 day";
        }
        elseif($count_type == 'week'){
            $type = "+1 week";
        }
        elseif($count_type == 'month'){
            $type = "+1 month";
        }
        elseif($count_type == 'year'){
            $type = "+1 year";
        }
        while (($min_date = strtotime($type, $min_date)) <= $max_date) {
            $i++;
        }
        return $i;
    }

    function formatMailDate($date){
        $year = date('Y',strtotime($date));
        $month = date('m',strtotime($date));
        $day = date('d',strtotime($date));
        $hour = date('H',strtotime($date));
        $minute = date('i',strtotime($date));
        $this_year = date('Y');
        $this_month = date('m');
        $this_day = date('d');
        $this_hour = date('H');
        $this_minute = date('i');
        $return = '';
        if(($year == $this_year) && ($month == $this_month) && ($day == $this_day)){
            if($hour == $this_hour){
                if($minute == $this_minute){
                    $return = 'now';
                }
                else{
                    $return = date('i',strtotime($date)).' min';
                }
            }
            else{
                $return = date('H:i a',strtotime($date));
            }
        }
        elseif(($year == $this_year) && ($month == $this_month) && ($day != $this_day)){
            $return = date('M j',strtotime($date));
        }
        elseif(($year == $this_year) && ($month != $this_month) && ($day != $this_day)){
            $return = date('M j',strtotime($date));
        }
        elseif(($year != $this_year) && ($month != $this_month) && ($day != $this_day)){
            $return = date('M j, Y',strtotime($date));
        }
        else{
            $return = $date;
        }

        return $return;
    }


    function formatNumber($value = 0, $type = 'number', $decimal_place = 2)
    {
        if ($type == 'money') {
            $num = number_format($value, $decimal_place, '.', ',');
        } else {
            $num = number_format($value, $decimal_place, '.', '');
        }

        return $num;
    }

    function to_sentence($str){
        return  ucwords(strtolower($str));
    }
}
